
package com.app.accelerometersensor;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;


public class MainActivity extends Activity
{
	private SensorManager sensorMgr;
	private Sensor accelerometerSensor;
	private Sensor orientationSensor;
	
	// accelerometer
	long lastTimeOfAccel = 0;
	float lastX;
	float lastY;
	float lastZ;
	
	float speed;
	
	// orientation
	long lastTimeOfOrient = 0;
	
	private static final int SHAKE_THRESHHOLD = 800;
	private static final int DATA_X = SensorManager.DATA_X;
	private static final int DATA_Y = SensorManager.DATA_Y;
	private static final int DATA_Z = SensorManager.DATA_Z;
	
	final SensorEventListener accelerometerEventListener = new SensorEventListener()
	{
		long lastTimeOfAccel = 0;
		private boolean mIsInitialized = false;
		
		private float mLastX = 0;
		private float mLastY = 0;
		private float mLastZ = 0;
		
		float [] gravity = { 0 , 0 , 0 };
		
		private final float alpha = (float) 0.8;
		private final float NOISE = (float) 2.0;
		
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy)
		{
			// TODO Auto-generated method stub			
		}

		@Override
		public void onSensorChanged(SensorEvent event)
		{
			// TODO Auto-generated method stub
			
			if ( event.sensor.getType() == Sensor.TYPE_ACCELEROMETER )
			{
	    		long currentTime = System.currentTimeMillis();
	    		long gabOfTime = ( currentTime - lastTimeOfAccel );
	    		
	    		if ( gabOfTime > 100 )
	    		{
	    			lastTimeOfAccel = currentTime;
	    			
					float x = event.values[0];
					float y = event.values[1];
					float z = event.values[2];
					
	    			float p1 = ( float )( Math.atan2( y , z ) * 180 / Math.PI );
	    			float r1 = ( float )( Math.atan2( x , z ) * 180 / Math.PI );
	    			
	    			float a = ( float )( Math.atan( ( x / Math.sqrt( ( y * y + z * z ) ) ) ) );
	    			float b = ( float )( Math.atan( ( y / Math.sqrt( ( x * x + z * z ) ) ) ) );
	    			float c = ( float )( Math.atan( ( Math.sqrt( ( y * y + x * x ) ) / z ) ) );

	    			gravity[0] = alpha * gravity[0] + ( 1 - alpha ) * event.values[0];
					gravity[1] = alpha * gravity[1] + ( 1 - alpha ) * event.values[1];
					gravity[2] = alpha * gravity[2] + ( 1 - alpha ) * event.values[2];
					

					x = event.values[0] - gravity[0];
					y = event.values[1] - gravity[1];
					z = event.values[2] - gravity[2];

	    			float p2 = ( float )( Math.atan2( y , z ) * 180 / Math.PI );
	    			float r2 = ( float )( Math.atan2( x , z ) * 180 / Math.PI );

	    			if ( mIsInitialized )
					{
						double deltaX = Math.abs( mLastX - x );
						if ( deltaX < NOISE )
						{
							deltaX = 0.0;
						}
						
						double deltaY = Math.abs( mLastY - y );
						if ( deltaY < NOISE )
						{
							deltaY = 0.0;
						}
	
						double deltaZ = Math.abs( mLastZ - z );
						if ( deltaZ < NOISE )
						{
							deltaZ = 0.0;
						}
						
						if ( deltaX > deltaY )
						{
							// Horizontal shake
						}
						else if ( deltaY > deltaX )
						{
							// Vertical shake
						}
						else if ( ( deltaZ > deltaX ) && ( deltaZ > deltaY ) )
						{
							// Z shake
						}
					}
					else
					{
						mIsInitialized = true;
					}

					mLastX = x;
					mLastY = y;
					mLastZ = z;

	    			{
	    				int iResID = 0;
	    				
	    				iResID = getResources().getIdentifier( "accel_sensor_x" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( event.values[0] ) );

	    				iResID = getResources().getIdentifier( "accel_sensor_y" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( event.values[1] ) );

	    				iResID = getResources().getIdentifier( "accel_sensor_z" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( event.values[2] ) );

	    				iResID = getResources().getIdentifier( "accel_roll" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( r1 ) );

	    				iResID = getResources().getIdentifier( "accel_pitch" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( p1 ) );

	    				iResID = getResources().getIdentifier( "accel_yaw" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( 0 ) );

	    				iResID = getResources().getIdentifier( "accel_r" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( a ) );

	    				iResID = getResources().getIdentifier( "accel_p" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( b ) );

	    				iResID = getResources().getIdentifier( "accel_y" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( c ) );

	    				iResID = getResources().getIdentifier( "accel_except_gravity_sensor_x" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( x ) );

	    				iResID = getResources().getIdentifier( "accel_except_gravity_sensor_y" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( y ) );

	    				iResID = getResources().getIdentifier( "accel_except_gravity_sensor_z" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( z ) );

	    				iResID = getResources().getIdentifier( "accel_except_gravity_roll" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( r2 ) );

	    				iResID = getResources().getIdentifier( "accel_except_gravity_pitch" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( p2 ) );

	    				iResID = getResources().getIdentifier( "accel_except_gravity_yaw" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( 0 ) );
	    			}
	    		}
			}
			
		}
	};

	final SensorEventListener accelerometerEventListener2 = new SensorEventListener()
	{
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy)
		{
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSensorChanged(SensorEvent event)
		{
			// TODO Auto-generated method stub
			
	    	if ( event.sensor.getType() == Sensor.TYPE_ACCELEROMETER )
	    	{
	    		long currentTime = System.currentTimeMillis();
	    		long gabOfTime = ( currentTime - lastTimeOfAccel );
	    		
	    		if ( gabOfTime > 100 )
	    		{
	    			lastTimeOfAccel = currentTime;
	    			
	    			float x = event.values[SensorManager.DATA_X];
	    			float y = event.values[SensorManager.DATA_Y];
	    			float z = event.values[SensorManager.DATA_Z];
	    			
	    			speed = Math.abs( x + y + z - lastX - lastY - lastZ ) / gabOfTime * 10000;
	    			
	    			float fPitch = ( float )( Math.atan2( y , z ) * 180 / Math.PI );
	    			float fRoll = ( float )( Math.atan2( x , z ) * 180 / Math.PI );
	    			
//	    			if ( speed > SHAKE_THRESHHOLD )
	    			{
	    				int iResID = 0;
	    				
	    				iResID = getResources().getIdentifier( "accel_sensor_x" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( x ) );

	    				iResID = getResources().getIdentifier( "accel_sensor_y" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( y ) );

	    				iResID = getResources().getIdentifier( "accel_sensor_z" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( z ) );

	    				iResID = getResources().getIdentifier( "accel_roll" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( fRoll ) );

	    				iResID = getResources().getIdentifier( "accel_pitch" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( fPitch ) );

	    				iResID = getResources().getIdentifier( "accel_yaw" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( 0 ) );
	    			}
	    			
	    			lastX = x;
	    			lastY = y;
	    			lastZ = z;
	    		}
	    	}
		}
	};
	
	final SensorEventListener orientationEventListener = new SensorEventListener()
	{
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy)
		{
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSensorChanged(SensorEvent event)
		{
			// TODO Auto-generated method stub
			
			if ( event.sensor.getType() == Sensor.TYPE_ORIENTATION )
			{
	    		long currentTime = System.currentTimeMillis();
	    		long gabOfTime = ( currentTime - lastTimeOfOrient );
	    		
	    		if ( gabOfTime > 100 )
	    		{
	    			lastTimeOfOrient = currentTime;
	    			
	    			float x = event.values[SensorManager.DATA_X];
	    			float y = event.values[SensorManager.DATA_Y];
	    			float z = event.values[SensorManager.DATA_Z];

	    			{
	    				int iResID = 0;
	    				
	    				iResID = getResources().getIdentifier( "orient_sensor_x" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( x ) );

	    				iResID = getResources().getIdentifier( "orient_sensor_y" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( y ) );

	    				iResID = getResources().getIdentifier( "orient_sensor_z" , "id" , "com.app.accelerometersensor" );
	    				( ( TextView )findViewById( iResID ) ).setText( String.valueOf( z ) );
	    			}
	    		}
			}
			
		}		
	};
	
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.fragment_main );
        
        sensorMgr = ( SensorManager )getSystemService( SENSOR_SERVICE );
        
        accelerometerSensor = sensorMgr.getDefaultSensor( Sensor.TYPE_ACCELEROMETER );
        orientationSensor = sensorMgr.getDefaultSensor( Sensor.TYPE_ORIENTATION ); 
    }
    
    public void onStart()
    {
    	super.onStart();
    	
    	if ( accelerometerSensor != null )
    	{
    		sensorMgr.registerListener( accelerometerEventListener , accelerometerSensor, SensorManager.SENSOR_DELAY_GAME );
    	}
    	
    	if ( orientationSensor != null )
    	{
    		sensorMgr.registerListener( orientationEventListener , orientationSensor , SensorManager.SENSOR_DELAY_GAME  );
    	}
    }
    
    public void onStop()
    {
    	super.onStop();
    	
    	if ( orientationSensor != null )
    	{
    		sensorMgr.unregisterListener( orientationEventListener );
    	}
    	
    	if ( accelerometerSensor != null )
    	{
    		sensorMgr.unregisterListener( accelerometerEventListener );
    	}
    }
}
